//
//  DailyInformationViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/12.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "DailyInformationViewController.h"
#import "ImageAndLabelCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "DailyInformationModel.h"
#import "HXMacro.h"
#import "HomePageDetailViewController.h"
#import "DailyInformationRes.h"

#define kHomePageCollectionViewImageAndLabelCellName @"HomePageCollectionViewImageAndLabelCellReuse"
@interface DailyInformationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) DailyInformationModel *model;

@end

@implementation DailyInformationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"每日资讯";
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Pop"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"LZUniversal_BarButtonItem_Search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
    self.navigationItem.leftBarButtonItem = back;
    self.navigationItem.rightBarButtonItem = search;
    _layout.minimumInteritemSpacing = 5;
    _layout.minimumLineSpacing = 5;
    _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"ImageAndLabelCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"6"};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=info&a=getlist" andBody:@"page=1" andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [DailyInformationModel modelWithDic:result];
        [_collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 20) / 3, (SCREEN_WIDTH - 20) / 3 + 40);
    return _layout.itemSize;
}
- (void)back:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)click:(UIBarButtonItem *)button {
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _model.res.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ImageAndLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHomePageCollectionViewImageAndLabelCellName forIndexPath:indexPath];
    cell.label.numberOfLines = 2;
    cell.label.font = [UIFont systemFontOfSize:15];
    cell.res = _model.res[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HomePageDetailViewController *vc = [[HomePageDetailViewController alloc] init];
    vc.numString = _model.res[indexPath.row].dailyInformation_id;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
